home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / MATV.ZIP / MATVTEST.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-11  |  5.7 KB  |  234 lines

  1.  
  2. // Matv - A Simple Matrix Class
  3.  
  4. // Version: 1.0
  5. // Author: Mark Von Tress, Ph.D.
  6. // Date: 10/11/92   
  7.  
  8. // Copyright(c) Mark Von Tress 1992
  9.  
  10.  
  11. // DISCLAIMER: THIS PROGRAM IS PROVIDED AS IS, WITHOUT ANY
  12. // WARRANTY, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
  13. // TO FITNESS FOR A PARTICULAR PURPOSE. THE AUTHOR DISCLAIMS
  14. // ALL LIABILITY FOR DIRECT OR CONSEQUENTIAL DAMAGES RESULTING
  15. // FROM USE OF THIS PROGRAM.
  16.  
  17. // test driver
  18.  
  19. #include "matv.h"
  20.  
  21. void foo(Matrix &arg)
  22. {
  23.    static int j = 0;
  24.    static Matrix zz = Fill(2, 2, 1);
  25.    // call zz.CannotAlias() to protect the storage in a
  26.    // static matrix.
  27.    zz.CannotAlias();
  28.    if (j < 2) cout << zz;
  29.    if (!j++) {
  30.       // test for side effects in reference matrices
  31.       cout << arg;
  32.       Matrix local(arg);
  33.       cout << local;
  34.       arg(1, 1) = 2000;
  35.       cout << local;
  36.    }
  37. }
  38.  
  39. #define bigg 25
  40. // run this 1000 times. Should bomb if there is a leak.
  41. Matrix TestForLeak(void)
  42. {
  43.    Matrix x = Inv(Fill(bigg, bigg, 1) +Ident(bigg));
  44.    foo(Fill(2, 2, 2));
  45.    // still works if you omit this call, but this is faster
  46.    x.release();
  47.    return x;
  48. }
  49. #undef bigg
  50.  
  51. void TestReg()
  52. {     
  53.    // do regression with normal equations or g2sweep
  54.    // read data
  55.    Matrix xy = Reada("catchv.dat");
  56.    xy.show("catchv.dat");
  57.  
  58.    // use normal equations
  59.    Matrix x = Submat(xy, 1, 2, xy.r, 3);
  60.    Matrix y = Submat(xy, 1, xy.c, xy.r, xy.c);
  61.    Matrix beta = Inv(Tran(x) *x) *Tran(x) *y;
  62.    beta.show("beta");
  63.   
  64.    // use sweep
  65.    Matrix xyp = Submat(xy, 1, 2, xy.r, xy.c);
  66.    Matrix xypxy = Tran(xyp) *xyp;
  67.    Matrix reg_comps = Sweep(1, xyp.c - 1, xypxy);
  68.    cout << reg_comps;
  69.  
  70.    beta = Submat(reg_comps, 1, reg_comps.c, reg_comps.r - 1, reg_comps.c);
  71.    beta.show("sweep beta");
  72.    float mse = reg_comps(reg_comps.r, reg_comps.c) / ((float) (xy.r - x.c));
  73.    cout << "mse: " << mse << "\n";
  74.  
  75. }
  76.  
  77. void TestArithOperators(void)
  78. {
  79.    Matrix a = Fill(5, 5, 0.5);
  80.    Matrix b = Fill(5, 5, 2);
  81.  
  82.    cout << "a+b\n" << (a + b);
  83.    cout << "a+5\n" << (a + 5);
  84.    cout << "5+a\n" << (5 + a);
  85.    cout << "a-b\n" << (a - b);
  86.    cout << "a-5\n" << (a - 5);
  87.    cout << "5-a\n" << (5 - a);
  88.    cout << "-a\n" << - a;
  89.    cout << "a*b\n" << a*b;
  90.    cout << "a*5\n" << a*5;
  91.    cout << "5*a\n" << 5*a;
  92.    cout << "a%b\n" << a % b;
  93.    cout << "a%5\n" << a % 5;
  94.    cout << "5%a\n" << 5 % a;
  95.    cout << "a/b\n" << a / b;
  96.    cout << "a/5\n" << a / 5;
  97.    cout << "5/a\n" << 5 / a;
  98. }
  99.  
  100. void TestArithAssignments(void)
  101. {
  102.    Matrix a = Fill(5, 5, 0.5);
  103.    Matrix b = Fill(5, 5, 2);
  104.    Matrix t = a;
  105.    a += b;
  106.    cout << "a+=b\n" << a;
  107.    a += 5;
  108.    cout << "a+=5\n" << a;
  109.    a = t;
  110.  
  111.    a -= b;
  112.    cout << "a-=b\n" << a;
  113.    a -= 5;
  114.    cout << "a-=5\n" << a;
  115.    a = t;
  116.  
  117.    a *= b;
  118.    cout << "a*=b\n" << a;
  119.    a *= 5;
  120.    cout << "a*=5\n" << a;
  121.    a = t;
  122.  
  123.    a %= b;
  124.    cout << "a%=b\n" << a;
  125.    a %= 5;
  126.    cout << "a%=5\n" << a;
  127.    a = t;
  128.  
  129.    a /= b;
  130.    cout << "a/=b\n" << a;
  131.    a /= 5;
  132.    cout << "a/=5\n" << a;
  133.    a = t;
  134.    t = a += b;
  135.    cout << "addition then assignment" << t;
  136.  
  137.    Matrix k = Fill(5,3,5);
  138.    t = Fill( 5, 5, 0.5 );
  139.    a = t;
  140.    t = a *= k;
  141.    cout << "multiplication then assignment" << t;
  142. }
  143.  
  144. /////////////////////////////////////////////////////
  145. //
  146. // The next section demonstrates a problem that
  147. // occurs with aliasing. If you return a matrix
  148. // that was passed as a reference, and you release
  149. // the deletion responsibility, then it will be
  150. // garbage later. You trip the garbage flag when you try
  151. // to use it after the matrix newley responsible
  152. // for deleting the aliased storage has actually
  153. // deleted the storage. The heap is notified of
  154. // the deletion so this problem can be detected.
  155. // see SetupVectors(), PurgeVectors(), and Garbage().
  156. //
  157. ////////////////////////////////////////////////////
  158.  
  159. Matrix passRef(Matrix &a)
  160. {
  161.    // release sets CanDelete to FFALSE, and the
  162.    // return value gets responsibility for deleting
  163.    // the storage in a.
  164.    a.release();
  165.    return a;
  166. }
  167.  
  168. void DontDoThis(Matrix &a)
  169. {
  170.    // alias problem  since release() passed deletion
  171.    // responsibility to AliasProblem. Note that
  172.    // AliasProblem vanishes at the function exit
  173.    // so the storage in a is now garbage!
  174.    // To prevent this problem call a.CannotAlias(), before
  175.    // entering this function. Or, do not release deletion
  176.    // responsibility for matrices that are passed in as
  177.    // references and returned at the end of a function.
  178.    Matrix AliasProblem = passRef(a);
  179. }
  180.  
  181. //////////////////////////////////////////////////////////////
  182.  
  183. main()
  184. {
  185.    ios::sync_with_stdio(); // Uncomment for SC++ Winc applications
  186.    Matrix xt;
  187.    for (int m = 0; m < 10; m++) {
  188.       xt = TestForLeak();
  189.       cout << m << " ";
  190.    }
  191.    cout << "\n";
  192.  
  193.    Matrix t(2, 2);
  194.    cout << t;
  195.    Matrix x = t;
  196.    cout << x;
  197.    x = Fill(3, 2, 1);
  198.    cout << x;
  199.    t = x;
  200.    cout << t;
  201.    t = x = Fill(3, 3, 3);
  202.    cout << x;
  203.    cout << t;
  204.  
  205.    // make a matrix from an array
  206.    float xx[6] = {0, 1, 2, 3, 4, 5};
  207.    Vector zz(6,xx);
  208.    cout << "zz\n" << zz;
  209.    t = Matrix( 2, 3, xx);
  210.    cout << "t\n" << t;
  211.  
  212.    // test array of matrices
  213.    int i, n = 5;
  214.    Matrix *c = new Matrix[n];
  215.    for (i = 0; i < n; i++) c[i] = Fill(i + 1, i + 1, (float) i);
  216.    for (i = 0; i < n; i++) cout << c[i];
  217.  
  218.    TestReg();
  219.    TestArithOperators();
  220.    TestArithAssignments();
  221.  
  222.    // test matrix write
  223.    cout << Inv(Ident(5) + Fill(5, 5, 1));
  224.    Writea("junk.mat", Inv(Ident(5) + Fill(5, 5, 1)));
  225.  
  226.    Matrix a = Fill(5, 5, 1);
  227.    // Uncomment the next line to prevent the alias problem in DontDoThis( a )
  228.    // a.CannotAlias();
  229.    DontDoThis(a);
  230.    a.Garbage("alias problem");
  231.  
  232.    return 0;
  233. }
  234.